package com.ujoin.dao.imp;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ujoin.bean.UserSetting;
import com.ujoin.dao.IUserSettingDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * UserSetting entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.ujoin.bean.UserSetting
 * @author MyEclipse Persistence Tools
 */

public class UserSettingDAO extends HibernateDaoSupport implements IUserSettingDAO {
	private static final Logger log = LoggerFactory
			.getLogger(UserSettingDAO.class);
	// property constants
	public static final String NEW_FOLLOWER_ALERT = "newFollowerAlert";
	public static final String NEW_PRIVATE_INVITATION_ALERT = "newPrivateInvitationAlert";
	public static final String NEW_ACTIVITY_INFO_ALERT = "newActivityInfoAlert";
	public static final String NEW_SYSTEM_INFO_ALERT = "newSystemInfoAlert";
	public static final String IS_INFO_PUBLIC = "isInfoPublic";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserSettingDAO#save(com.ujoin.bean.UserSetting)
	 */
	@Override
	public void save(UserSetting transientInstance) {
		log.debug("saving UserSetting instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserSettingDAO#delete(com.ujoin.bean.UserSetting)
	 */
	@Override
	public void delete(UserSetting persistentInstance) {
		log.debug("deleting UserSetting instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserSettingDAO#findById(java.lang.Integer)
	 */
	@Override
	public UserSetting findById(java.lang.Integer id) {
		log.debug("getting UserSetting instance with id: " + id);
		try {
			UserSetting instance = (UserSetting) getHibernateTemplate().get(
					"com.ujoin.bean.UserSetting", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserSettingDAO#findByExample(com.ujoin.bean.UserSetting)
	 */
	@Override
	public List findByExample(UserSetting instance) {
		log.debug("finding UserSetting instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserSettingDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding UserSetting instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from UserSetting as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserSettingDAO#findByNewFollowerAlert(java.lang.Object)
	 */
	@Override
	public List findByNewFollowerAlert(Object newFollowerAlert) {
		return findByProperty(NEW_FOLLOWER_ALERT, newFollowerAlert);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserSettingDAO#findByNewPrivateInvitationAlert(java.lang.Object)
	 */
	@Override
	public List findByNewPrivateInvitationAlert(Object newPrivateInvitationAlert) {
		return findByProperty(NEW_PRIVATE_INVITATION_ALERT,
				newPrivateInvitationAlert);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserSettingDAO#findByNewActivityInfoAlert(java.lang.Object)
	 */
	@Override
	public List findByNewActivityInfoAlert(Object newActivityInfoAlert) {
		return findByProperty(NEW_ACTIVITY_INFO_ALERT, newActivityInfoAlert);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserSettingDAO#findByNewSystemInfoAlert(java.lang.Object)
	 */
	@Override
	public List findByNewSystemInfoAlert(Object newSystemInfoAlert) {
		return findByProperty(NEW_SYSTEM_INFO_ALERT, newSystemInfoAlert);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserSettingDAO#findByIsInfoPublic(java.lang.Object)
	 */
	@Override
	public List findByIsInfoPublic(Object isInfoPublic) {
		return findByProperty(IS_INFO_PUBLIC, isInfoPublic);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserSettingDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all UserSetting instances");
		try {
			String queryString = "from UserSetting";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserSettingDAO#merge(com.ujoin.bean.UserSetting)
	 */
	@Override
	public UserSetting merge(UserSetting detachedInstance) {
		log.debug("merging UserSetting instance");
		try {
			UserSetting result = (UserSetting) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserSettingDAO#attachDirty(com.ujoin.bean.UserSetting)
	 */
	@Override
	public void attachDirty(UserSetting instance) {
		log.debug("attaching dirty UserSetting instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserSettingDAO#attachClean(com.ujoin.bean.UserSetting)
	 */
	@Override
	public void attachClean(UserSetting instance) {
		log.debug("attaching clean UserSetting instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IUserSettingDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (IUserSettingDAO) ctx.getBean("UserSettingDAO");
	}
}